package cn.lesper.process;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import cn.lesper.model.ColumnEntity;
import cn.lesper.model.TableEntity;
import org.w3c.dom.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

//此层次解析xml
public abstract class AbstractExtensible {

    //用于测试的xml
    static String testPath = "C:\\Users\\surge\\Desktop\\java\\myworks\\quattro\\native-jdbc\\src\\main\\resources\\test.xml";

    File file = new File(testPath);
    Element element = null;
    DocumentBuilder documentBuilder = null;
    DocumentBuilderFactory documentBuilderFactory = null;

    TableEntity tableEntity = new TableEntity();
    ColumnEntity columnEntity = new ColumnEntity();
    List<ColumnEntity> columnEntityList = new ArrayList<>();

    /**
     * @note 解析xml.entity并填充到实体
     * @return tableEntity
     * @throws Exception
     */
    public TableEntity addTableEntity() throws Exception {

        documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        element = document.getDocumentElement();

        //先填充表
        NodeList childNodes = element.getElementsByTagName("entity").item(0).getChildNodes();

        //遍历entity
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);

            //表名
            tableEntity.setTableName(node.getNodeName());

            //迭代列名
            NodeList nodeList = node.getChildNodes();

            for (int j = 0; j < nodeList.getLength(); j++) {
                Node cloumn = nodeList.item(j);

                //过滤杂项属性
                if ("#text".equals(cloumn.getNodeName())) {
                    continue;
                }

                //列名
                columnEntity.setCloumnName(cloumn.getNodeName());

                //添加附加验证属性
                try {
                    NamedNodeMap attrs = cloumn.getAttributes();
                    System.out.println("============>INFO:CLOUMN:"+cloumn.getNodeName() + ":ATTR:" + attrs.getLength());
                    for (int k = 0; k < attrs.getLength(); k++) {
                        Attr attr = (Attr) attrs.item(k);
                        System.out.println("============>INFO:属性添加：" + attr.getName() + "VALUE:" + attr.getValue());
                        if ("type".equals(attr.getName())) {
                            columnEntity.setDataType(attr.getValue());
                        } else if ("cfa".equals(attr.getName())) {
                            columnEntity.setCfa(attr.getValue());
                        }else if ("message".equals(attr.getName())) {
                            columnEntity.setMessage(attr.getValue());
                        } else if ("groups".equals(attr.getValue())) {
                            columnEntity.setGroups(attr.getValue());
                        } else if ("defVal".equals(attr.getName())) {
                            columnEntity.setDefVal(attr.getValue());
                        } else if ("ignore".equals(attr.getName())) {
                            columnEntity.setIgnore(attr.getValue());
                        }
                    }

                } catch (Exception ex) {
                    // ex.printStackTrace();
                    System.out.println("============>INFO:该节点无属性");
                }
                columnEntityList.add(columnEntity);
                columnEntity = new ColumnEntity();
            }
            tableEntity.setCloumn(columnEntityList);
        }

        return tableEntity;
    }

}
